﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using SalePromotion.Service;
using eBay.Service.Call;
using eBay.Service.Core.Sdk;
using eBay.Service.Core.Soap;

namespace SalePromotion.eBay
{

    public class eBaySession
    {

        public ApiContext ApiContext
        {
            get
            {
                if (!HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    throw new Exception("user should logon firstly.");
                }

                return ApiContextLoader.LoadApiContext(HttpContext.Current.User.Identity.Name);

            }
        }


        public static string GetSessionId(ApiContext context, string ruName)
        {
            return new GetSessionIDCall(context).GetSessionID(ruName);
        }
        public StoreCustomCategoryTypeCollection GeteBayStoreCategories()
        {
            var store = new GetStoreCall(ApiContext).GetStore();
            if (store == null) return null;
            return store.CustomCategories;
        }

        public StoreCustomCategoryTypeCollection GetSortby()
        {
            var store = new GetStoreCall(ApiContext).GetStore();
            if (store == null) return null;
            return store.CustomCategories;
        }


        public ItemTypeCollection CallGetSellerList(DateTime endDateTimeFrom, DateTime endDateTimeTo)
        {
            try
            {
                var api = new GetSellerListCall(ApiContext);
                //api.IncludeWatchCount = true;
                api.DetailLevelList.Add(DetailLevelCodeType.ReturnAll);
                api.EndTimeFilter = new TimeFilter(endDateTimeFrom, endDateTimeTo);

                var pagination = new PaginationType();
                pagination.EntriesPerPage = 100;
                api.Pagination = pagination;

                int pageNum = 1;
                bool hasMore = true;
                var itemCollection = new ItemTypeCollection();
                while (hasMore)
                {
                    pagination.PageNumber = pageNum++;
                    itemCollection.AddRange(api.GetSellerList());
                    hasMore = api.ApiResponse.HasMoreItems;
                }
                return itemCollection;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public PromotionalSaleStatusCodeType SetPromotionalSale(ModifyActionCodeType action, PromotionalSaleType promationSale)
        {
            var api = new SetPromotionalSaleCall(ApiContext);
            api.SetPromotionalSale(action, promationSale);
            promationSale.PromotionalSaleID = api.PromotionalSaleID;
            return api.Status;
        }

        public PromotionalSaleStatusCodeType SetPromotionalSaleListings(long promotionalSaleID, ModifyActionCodeType action, ItemIDArrayType promotionalSaleItemIDArrayList, long storeCategoryID, long categoryID, bool allFixedPriceItems, bool allStoreInventoryItems, bool allAuctionItems)
        {
            var api = new SetPromotionalSaleListingsCall(ApiContext);
            api.PromotionalSaleID = promotionalSaleID;
            api.Action = action;
            if (promotionalSaleItemIDArrayList.ItemID.Count > 0)
                api.PromotionalSaleItemIDArrayList = promotionalSaleItemIDArrayList;
            if (storeCategoryID > 0 && allStoreInventoryItems)
                api.StoreCategoryID = storeCategoryID;
            if (categoryID > 0)
                api.CategoryID = categoryID;

            api.AllFixedPriceItems = allFixedPriceItems;
            api.AllStoreInventoryItems = allStoreInventoryItems;
            api.AllAuctionItems = allAuctionItems;
            api.Execute();
            return api.Status;
        }

        public PromotionalSaleStatusCodeType RemovePromotionalSaleListings(long promotionalSaleId,ItemIDArrayType removedPromotionalSaleItemIdArrayList)
        {
            var api = new SetPromotionalSaleListingsCall(ApiContext);
            api.PromotionalSaleID = promotionalSaleId;
            api.Action = ModifyActionCodeType.Delete;
            if (removedPromotionalSaleItemIdArrayList.ItemID.Count > 0)
                api.PromotionalSaleItemIDArrayList = removedPromotionalSaleItemIdArrayList;
            api.Execute();
            return api.Status;
        }


        public ItemTypeCollection GetActiveListings(ListingTypeCodeType listingTypeCodeType, ItemSortTypeCodeType sort)
        {
            try
            {
                var api = new GetMyeBaySellingCall(ApiContext);
                //api.IncludeWatchCount = true;
                api.DetailLevelList.Add(DetailLevelCodeType.ReturnAll);
                api.ActiveList =new ItemListCustomizationType();
                api.ActiveList.Include = true;
                api.ActiveList.ListingType = listingTypeCodeType;
                api.ActiveList.Sort = sort;
                var pagination = new PaginationType();
                pagination.EntriesPerPage = 100;
                api.ActiveList.Pagination = pagination;

                int pageNum = 1;
                bool hasMore = true;
                var itemCollection = new ItemTypeCollection();
                while (hasMore)
                {
                    pagination.PageNumber = pageNum++;
                    api.Execute();
                    if (api.ActiveListReturn == null) break;
                    itemCollection.AddRange(api.ActiveListReturn.ItemArray);
                    hasMore = api.ActiveListReturn.ItemArray.Count == 100;
                }
                return itemCollection;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}